<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/19
 * Time: 10:52
 */

namespace app\course\service;


use app\classes\model\ClassModel;

class CourseTeacherStatisticsService
{
    public function getList($filter,$isPage = false)
    {
        //店铺ID
        $where=[
            'p.delete_time'=>0,
            'u.user_status'=>1,
            'p.shop_id'=> $filter['admin_shop_id'],
            'u.shop_id'=> $filter['admin_shop_id'],
        ];
        $category=empty($filter['category'])?0:intval($filter['category']);
        $field='sum(p.current_num) pcurrent_num,count(distinct p.id) pnum,min(p.create_time) pcreate_time,u.user_login,u.id uid,u.user_email,u.mobile,u.create_time ucreate_time';
        $class_model=new ClassModel();

        if(!empty($filter['keyword']))
        {
            //关键字搜索
            //用户名称 用户邮箱 用户手机
            $where['u.user_login|u.user_email|u.mobile']=['like',"%".trim($filter['keyword'])."%"];
        }
        $data=$class_model->alias('p')->field($field)
            ->join('user u','u.id=p.teacher_id')
            ->where($where)
            ->group('u.user_login')
            ->order('p.id','DESC')
            ->paginate(15,false,['query' => $filter]);

        return $data;

    }
    public function getClassList($tid,$filter)
    {
        //店铺ID
        $where=[
            'p.delete_time'=>0,
            's.delete_time'=>0,
            'c.delete_time'=>0,
            'p.teacher_id'=>$tid,
        ];
        if(!empty($filter['keyword']))
        {
            //关键字搜索
            //用户名称 用户邮箱 用户手机
            $where['c.course_name|s.name|p.name']=['like',"%".trim($filter['keyword'])."%"];
        }
        $class_model=new ClassModel();

        $field='p.id pid,p.name pname,p.current_num ,
                p.start_lesson_time,p.class_start_week,p.class_time_start,p.class_time_end,
                c.course_name,s.name sname';

        $data=$class_model->alias('p')->field($field)
            ->join('course c','c.id=p.course_id')
            ->join('school s','s.id=p.school_id')
            ->where($where)
            ->order("pid DESC")
            ->paginate(10);
        foreach($data as $j=>$v){
                $getCourseTime=$class_model->getCourseTime($v['pid']);
                $date_length=count($getCourseTime);
                $last_lesson_day=$getCourseTime[$date_length-1]['date'];
                $data[$j]['last_lesson_day']=$last_lesson_day;
                $count=explode(',',$v['class_time_start']);
            if(count($count)==1)
            {
                switch(intval($v['class_start_week']))
                {
                    case 1:
                        $class_start_week='每周一';
                        break;
                    case 2:
                        $class_start_week='每周二';
                        break;
                    case 3:
                        $class_start_week='每周三';
                        break;
                    case 4:
                        $class_start_week='每周四';
                        break;
                    case 5:
                        $class_start_week='每周五';
                        break;
                    case 6:
                        $class_start_week='每周六';
                        break;
                    case 7:
                        $class_start_week='每周日';
                        break;
                }
                $data[$j]['shangke']=$class_start_week.' &nbsp;&nbsp;  '.$v['class_time_start'].'-'.$v['class_time_end'];
            }
            if(count($count)>1)
            {
                $data[$j]['shangke']='';
                $week=explode(',',$v['class_start_week']);
                $end=explode(',',$v['class_time_end']);
                foreach($count as $k=>$val)
                {
                    switch(intval($week[$k]))
                    {
                        case 1:
                            $class_start_week='每周一';
                            break;
                        case 2:
                            $class_start_week='每周二';
                            break;
                        case 3:
                            $class_start_week='每周三';
                            break;
                        case 4:
                            $class_start_week='每周四';
                            break;
                        case 5:
                            $class_start_week='每周五';
                            break;
                        case 6:
                            $class_start_week='每周六';
                            break;
                        case 7:
                            $class_start_week='每周日';
                            break;
                    }
                    $data[$j]['shangke'].=$class_start_week.' &nbsp;&nbsp; '.$count[$k].'-'.$end[$k]."<br /> ";
                }
            }
            }
        return $data;
    }
    public  function getCourseList($tid)
    {


        //店铺ID
         $time=time();
        $where=[
            'p.delete_time'=>0,
            's.delete_time'=>0,
            'c.delete_time'=>0,
            'p.teacher_id'=>$tid,

        ];
        $class_model=new ClassModel();

        $field='p.id pid,p.name pname,p.current_num ,p.teacher_id,
                p.start_lesson_time,p.class_start_week,
                p.class_time_start,p.class_time_end,
                c.course_name,s.name sname,r.name rname
                ,cs.date,cs.start_time,cs.end_time,cs.schedule,tl.supply_teacher,u.user_login uname';

        $data=$class_model->alias('p')->field($field)
            ->join('course c','c.id=p.course_id')
            ->join('school s','s.id=p.school_id')
            ->join('classroom r','r.id=p.classroom_id')
            ->join('course_schedule cs','cs.class_id=p.id and cs.teacher_id=p.teacher_id')
            ->join('user u','u.id=p.teacher_id')
            ->join('teacher_leave tl','tl.teacher_id=cs.teacher_id and tl.leave_date=cs.date',$type='left')
            ->where($where)
            ->where('cs.date','>',$time)
            ->order("cs.date")
            ->paginate(10);
//        dump($data);
//        exit();
        return $data;
    }
    public  function getCourseListExe($tid)
    {

        $time = time();
        $where = [
            'p.delete_time' => 0,
            's.delete_time' => 0,
            'c.delete_time' => 0,
            'p.teacher_id' => $tid,

        ];
        $class_model = new ClassModel();

        $field = 'p.id pid,p.name pname,p.current_num ,p.teacher_id,
                p.start_lesson_time,p.class_start_week,
                p.class_time_start,p.class_time_end,
                c.course_name,s.name sname,r.name rname
                ,cs.date,cs.start_time,cs.end_time,cs.schedule,tl.supply_teacher,u.user_login uname';;

        $data = $class_model->alias('p')->field($field)
            ->join('course c', 'c.id=p.course_id')
            ->join('school s', 's.id=p.school_id')
            ->join('classroom r', 'r.id=p.classroom_id')
            ->join('course_schedule cs', 'cs.class_id=p.id and cs.teacher_id=p.teacher_id')
            ->join('user u','u.id=p.teacher_id')
            ->join('teacher_leave tl','tl.teacher_id=cs.teacher_id and tl.leave_date=cs.date',$type='left')
            ->where($where)
            ->where('cs.date', '>', $time)
            ->order("cs.date")
            ->select()
            ->toArray();

        return $data;
    }
}